/**
 * @author 可乐加糖
 */

(function(){
	var x=y=0;
	var xDirection = 1;
	var yDirection = 1;
	var image = new Image();
	image.src = "jsplatformer1-smiley.jpg";
	var canvas = document.getElementById("canvas");
	var context2D = canvas.getContext('2d');
	var FPS = 800;
	
	//画图函数
	var drawImg = function(){
		clearCanvas();
		context2D.drawImage(image,x,y);
		//边界值判断
		x += xDirection;
		y += yDirection;
		//判断画板边界值
		if(y>canvas.height-150){
			yDirection += -1;
		}else if(x>canvas.width-150){
			xDirection += -1;
		}else if(y<0){
			yDirection += 1
		}else if(x<0){
			xDirection += 1;
		}
	}
	//清理画布
	var clearCanvas = function(){
		context2D.clearRect(0,0,canvas.width,canvas.height);
	}
	//初始函数值
	var init = function(){
		window.setInterval(drawImg,1000/FPS);
	}
	init();
})();
